﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.IO.Compression;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Media.Imaging;

namespace RemoteDesktop
{
    internal class ImgUtils
    {
        public static bool isPress = true;
        public static BitmapSource ConvertBitmapToBitmapSource(Bitmap bitmap)
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                // 将 Bitmap 保存到内存流
                bitmap.Save(memoryStream, ImageFormat.Jpeg);
                memoryStream.Seek(0, SeekOrigin.Begin);

                // 创建 BitmapSource
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = memoryStream;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();

                return bitmapImage;
            }
        }

        /// <summary>
        /// 截取一帧图像并保存
        /// </summary>
        /// <param name="filePath">图像保存路径</param>
        /// <param name="fileName">保存的图像文件名</param>
        /// <param name="bitmap">保存的图像</param>
        /// <returns>如果保存成功，则返回完整路径，否则为 null</returns>
        public static string CaptureImage(string filePath, string fileName = null, Bitmap bitmap = null)
        {
            if (bitmap == null) return null;
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            try
            {
                if (fileName == null)
                {
                    fileName = DateTime.Now.ToString("yyyyMMddHHmmss");
                }
                else
                {
                    fileName += DateTime.Now.ToString("yyyyMMddHHmmss");
                }
                string fullPath = Path.Combine(filePath, fileName + ".jpg");
                bitmap.Save(fullPath, ImageFormat.Jpeg);
                bitmap.Dispose();
                return fullPath;
            }
            catch (Exception e)
            {
                _ = MessageBox.Show(e.Message);
                return null;
            }
        }
        public static byte[] Compress(byte[] data)
        {
            if (!isPress)
            {
                return data;
            }
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (GZipStream gzipStream = new GZipStream(memoryStream, CompressionMode.Compress))
                {
                    gzipStream.Write(data, 0, data.Length);
                }
                return memoryStream.ToArray();
            }
        }
        public static byte[] Decompress(byte[] compressedData)
        {
            if (!isPress)
            {
                return compressedData;
            }
            using (MemoryStream memoryStream = new MemoryStream(compressedData))
            {
                using (GZipStream gzipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
                {
                    using (MemoryStream outputStream = new MemoryStream())
                    {
                        gzipStream.CopyTo(outputStream);
                        return outputStream.ToArray();
                    }
                }
            }
        }
        public static ImageCodecInfo GetEncoderInfo(String mimeType)
        {
            ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
            foreach (ImageCodecInfo encoder in encoders)
            {
                if (encoder.MimeType == mimeType)
                    return encoder;
            }
            return null;
        }
    }
}
